<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
            :confirmButtonText="false"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules" label-position="top">
                <el-form-item label="调解编号" prop="mediationNumber">
                    <el-input v-model="formData.mediationNumber" />
                </el-form-item>
                <el-form-item label="案件名称" prop="nameCase">
                    <el-input v-model="formData.nameCase" />
                </el-form-item>
                <el-form-item label="案件编号" prop="caseNumber">
                    <el-input v-model="formData.caseNumber" />
                </el-form-item>
                <el-form-item label="调解员" prop="courtClerk">
                    <el-input v-model="formData.courtClerk" />
                </el-form-item>
                <el-form-item label="案件类型" prop="caseType">
                    <el-select class="flex-1" v-model="formData.caseType" >
                        <el-option
                            v-for="(item, index) in dictData.case_type"
                            :key="index"
                            :label="item.name"
                            :value="parseInt(item.value)"
                            clearable
                            :disabled="!item.status"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="调解结果" prop="mediationResult">
                    <el-select class="flex-1" v-model="formData.mediationResult" >
                        <el-option
                            v-for="(item, index) in dictData.mediation_result"
                            :key="index"
                            :label="item.name"
                            :value="parseInt(item.value)"
                            clearable
                            :disabled="!item.status"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="受理时间" prop="receptionTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.receptionTime"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择受理时间"
                    />
                </el-form-item>
                <el-form-item label="完成时间" prop="completionTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.completionTime"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择完成时间"
                    />
                </el-form-item>
                <el-form-item label="视频地址" prop="video">
                    <el-input v-model="formData.video" />
                </el-form-item>
                <el-form-item label="调解书 " prop="mediationDocument">
                    <el-input v-model="formData.mediationDocument" />
                </el-form-item>
                <el-form-item label="案件评判" prop="caseJudgment">
                    <el-input v-model="formData.caseJudgment" placeholder="请输入案件评判" />
                </el-form-item>
                <el-form-item label="司法确认书" prop="judicialConfirmation">
                    <el-input v-model="formData.judicialConfirmation" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="创建人" prop="founder">
                    <el-input v-model="formData.founder" placeholder="请输入创建人" />
                </el-form-item>
                <el-form-item label="创建时间" prop="creationTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.creationTime"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择创建时间"
                    />
                </el-form-item>
                <el-form-item label="是否是第一次调解" prop="isFirstMediation">
                    <el-radio-group v-model="formData.isFirstMediation">
                        <el-radio
                            v-for="(item, index) in dictData.yes1_no0"
                            :key="index"
                            :label="item.value"
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否司法处理" prop="isJudicialTreatment">
                    <el-radio-group v-model="formData.isJudicialTreatment">
                        <el-radio
                            v-for="(item, index) in dictData.yes1_no0"
                            :key="index"
                            :label="item.value"
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
<!--                    <el-input v-model="formData.isJudicialTreatment" placeholder="请输入是否司法处理0否 1是" />-->
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  recordEdit, recordAdd, recordDetail } from '@/api/record'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '详情' : '新增调解记录'
})

const formData = reactive({
    mediationNumber: '',
    nameCase: '',
    caseNumber: '',
    courtClerk: '',
    courtId: '',
    caseType: '',
    mediationResult: '',
    receptionTime: '',
    completionTime: '',
    video: '',
    mediationDocument: '',
    caseJudgment: '',
    customerNumber: '',
    judicialConfirmation: '',
    remark: '',
    founder: '',
    creationTime: '',
    modifier: '',
    modificationTime: '',
    status: '',
    isFirstMediation: '',
    isJudicialTreatment: '',
})

const formRules = {
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await recordEdit(data) : await recordAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await recordDetail({
        mediationNumber: row.mediationNumber
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
